<?php


namespace app\common\handler;


class TimeHandler
{
    /**
     * @param $time
     * @return bool|array
     * 将秒数转换为时间（年、天、小时、分、秒）
     */
    public static function Sec2Time($time)
    {
        if (is_numeric($time)) {
            $value = array(
                "years" => 0, "days" => 0, "hours" => 0,
                "minutes" => 0, "seconds" => 0,
            );
            if ($time >= 31556926) {
                $value["years"] = floor($time / 31556926);
                $time = ($time % 31556926);
            }
            if ($time >= 86400) {
                $value["days"] = floor($time / 86400);
                $time = ($time % 86400);
            }
            if ($time >= 3600) {
                $value["hours"] = floor($time / 3600);
                $time = ($time % 3600);
            }
            if ($time >= 60) {
                $value["minutes"] = floor($time / 60);
                $time = ($time % 60);
            }
            $value["seconds"] = floor($time);
            return (array)$value;
            //$t=$value["years"] ."年". $value["days"] ."天"." ". $value["hours"] ."小时". $value["minutes"] ."分".$value["seconds"]."秒";

        } else {
            return (bool)FALSE;
        }
    }

    public static function getTimeStr2Day($time)
    {

        $arr = self::Sec2Time($time);

        return $arr['days'] . '天' . $arr['hours'] . '小时';
    }


    //时间戳 返 小时 分钟
    public static function getTimeHour($time)
    {
        $hours = 0 ;
        if ($time >= 3600) {
            $hours = floor($time / 3600);
            $time = ($time % 3600);
        }
        $minutes = 0;
        if ($time >= 60) {
            $minutes = floor($time / 60);
        }

        $string1 =  '' ;
        if($hours > 0){
            $string1 = $hours . '小时';
        }
        $string2 =  '' ;
        if($minutes > 0){
            $string2 = $minutes . '分钟';
        }
        return  $string1 . $string2 ;
    }


    public static function getStrtotime($time, $format = 'Y-m', $ms = false)
    {

        $timeArr = [];

        $timeArr['start'] = strtotime($time) ? strtotime($time) : time();

        switch ($format) {
            case 'Y-m-d' :
                $timeArr['end'] = strtotime('+1 day', $timeArr['start']);
                break;
            case 'Y-m' :
                $timeArr['end'] = strtotime('+1 month', $timeArr['start']);
                break;
            case 'Y' :
                $timeArr['end'] = strtotime('+1 year', $timeArr['start']);
                break;
            default:
                $timeArr['end'] = strtotime('+1 month', $timeArr['start']);
                break;
        }
        $timeArr['end'] = $timeArr['end'] - 1;

        if ($ms) {
            $timeArr['start'] = $timeArr['start'] * 1000;
            $timeArr['end'] = $timeArr['end'] * 1000;
        }
        return $timeArr;

    }

    /**
     * 毫秒转日期
     */
    public static function getMsecToMescdate($msectime, $format = 'Y-m-d H:i:s')
    {
        $msectime = $msectime * 0.001;
        if (strstr($msectime, '.')) {
            sprintf("%01.3f", $msectime);
            list($usec, $sec) = explode(".", $msectime);
            $sec = str_pad($sec, 3, "0", STR_PAD_RIGHT);
        } else {
            $usec = $msectime;
            $sec = "000";
        }
        $date = date($format, $usec);
        return $mescdate = str_replace('x', $sec, $date);
    }


    /**
     *  时间戳变成指定格式
     */
    public static function getStrtotimeDate($strtotime, $format = 'Y-m-d H:i:s')
    {
        if ($strtotime) {
            return date($format, $strtotime);
        }
        return "";
    }

    /**
     *  时间格式变成指定格式
     */
    public static function ReplaceFormat($time, $format = 'Y-m-d H:i:s')
    {
        if ($time) {
            return date($format, strtotime($time));
        }
        return "";
    }


    /**
     * @param $month
     * @param string $format
     * @return array
     * 获取某月的每天
     */
    public static function everyDay($month,$format='Y-m-d')
    {

        $start = strtotime(date('Y-m-d', strtotime($month)));
        $end = strtotime(date('Y-m-d', strtotime('+ 1 month', strtotime($month)) - 1));
        $arr = [];
        while ($start <= $end) {
            $arr[] = date($format, $start);
            $start = strtotime("+ 1 day", $start);
        }
        return $arr;
    }


    /**
     * @param $year
     *  * @param string $format
     * @return array
     * 获取某年的每月
     */
    public static function everyMonth($year,$format='Y-m')
    {

        $start = strtotime(date('Y-01', strtotime($year)));
        $end = strtotime(date('Y-m', strtotime('+ 1 year', $start- 1) ));
        $arr = [];
        while ($start <= $end) {
            $arr[] = date($format, $start);
            $start = strtotime("+ 1 month", $start);
        }
        return $arr;
    }


    //根据传入时间获取最近某段时间（小时；月）
    public static function nearTime($time = '', $num, $type = 'D',$format='Y-m-d H:i:s')
    {
        $time = $time ? $time : date('Y-m-d H:i:s');

        $end = strtotime($time);

        switch ($type) {
            case 'H' :
                $timeType = 'hour';
                break;
            case 'D' :
                $timeType = 'day';
                break;
            case 'M' :
                $timeType = 'month';
                break;
            case 'Y' :
                $timeType = 'year';
                break;
        }

        $start = strtotime('-  ' . $num . ' ' . $timeType, $end);

        $arr = [];
        while ($start <= $end) {
            $arr[] = date($format, $start);
            $start = strtotime("+ 1 " . $timeType, $start);
        }

        return $arr;
    }


    /**
     * @param $time 秒
     * @param int $num 时分
     *
     */
   public static  function getAssignTime($time,$num=60,$precision=0){
        return $time > 0 ? round($time/$num,$precision) : 0;
   }


    /**
     * @param $time
     * @return string
     * 获取指定日期是
     */
    public static  function getWeek($time){
        $weekarray=array("日","一","二","三","四","五","六");
        return "星期".$weekarray[date("w",strtotime($time))];
    }


    /**
     * @param $time
     * @param string $format 传入的什么格式就返回什么格式
     * @return false|string
     * 获取日期环比 或同比的日期
     */
    public static  function getChainComparison($time,$type = 'M',$format='Y-m'){

        switch ($type) {
            case 'D' :
                $timeType = 'day';
                break;
            case 'M' :
                $timeType = 'month';
                break;
            case 'Y' :
                $timeType = 'Year';
                break;
            default :
                $timeType = 'month';
                break;
        }
        return date($format,strtotime('- 1 '.$timeType,strtotime($time)));
    }


    /**
     * @param $time  过期时间
     *  判断时间是否过期
     */
    public static function getTimeOverdue($time){
        if(time() <= strtotime($time)){
            return true ;
        }
        return false;
    }

}